using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPCoco;
using WPCoco.Specialized;
using WPCoco.Tilesets;
using Microsoft.Xna.Framework;
using WPCoco.Controllers;
using Microsoft.Xna.Framework.Input.Touch;
using WPCoco.Graphics;

namespace TiledLayerDemo.Scenes
{
    public class MainScene : Scene
    {
        public MainScene()
        {
            Tileset tileset = TilesetCache.Load("TileSet");

            layer = new TiledLayer();
            layer.Tileset = tileset;
            layer.Size = new Size(512, 512);
            layer.TileSize = new Vector2(40, 40);

            // Set some content
            for (int i = 0; i < 512; i++)
            {
                for (int j = 0; j < 512; j++)
                {
                    layer[i, j] = (i ^ j) % tileset.Count;
                }
            }

            AddNode(layer);

            ControllerManager.Shared.TouchScreen.Enabled = true;
            ControllerManager.Shared.TouchScreen.StartDrag += new EventHandler<CustomEventArgs<GestureSample>>(TouchScreen_StartDrag);
            ControllerManager.Shared.TouchScreen.Dragging += new EventHandler<CustomEventArgs<GestureSample>>(TouchScreen_Dragging);
        }

        TiledLayer layer;

        Vector2 lastPoint, downPoint;

        void TouchScreen_StartDrag(object sender, CustomEventArgs<GestureSample> e)
        {
            lastPoint = layer.Position;
            downPoint = e.Value.Position;
        }

        void TouchScreen_Dragging(object sender, CustomEventArgs<GestureSample> e)
        {
            Vector2 offset = e.Value.Position - downPoint;
            Matrix m = MatrixHelper.GetViewWorldMatrix(layer);
            offset = Vector2.TransformNormal(offset, m);
            layer.Position = lastPoint + offset;
        }

    }
}
